﻿using System;
using System.Linq;
using System.Data.Linq;
using UniMana.Data;
using UniMana.Utils.Configs;

namespace UniMana.Data
{
    public static class Log_TranferData
    {
        public static bool m_bFlag = false;

        /// <summary>
        /// Lấy danh sách phiếu yêu cầu kèm theo khoa đề xuất
        /// </summary>
        /// <param name="nLog_Tranfer"></param>
        /// <param name="nTraining_TrainingProgram"></param>
        /// <param name="nSystem_User"></param>
        /// <param name="nPersonnel_Personnel"></param>
        /// <param name="nCatalogue_Major"></param>
        /// <returns></returns>
        /// 
        public static IQueryable GetListTranferForCensorship(this Table<Log_Tranfer> nLog_Tranfer,
            Table<Training_TrainingProgram> nTraining_TrainingProgram,
            Table<System_User> nSystem_User,
            Table<Personnel_Personnel> nPersonnel_Personnel,
            Table<Catalogue_Major> nCatalogue_Major)
        {
            var values = (from t in nTraining_TrainingProgram
                          join t0 in nLog_Tranfer on new { Training_TrainingProgramKey = t.Training_TrainingProgramKey } equals new { Training_TrainingProgramKey = Convert.ToInt32(t0.Training_TrainingProgramKeyBefore) }
                          join t1 in nCatalogue_Major on new { Catalogue_MajorKey = Convert.ToInt32(t.Catalogue_MajorKey) } equals new { Catalogue_MajorKey = t1.Catalogue_MajorKey }
                          join t2 in nSystem_User on t0.RequireUser equals t2.System_UserKey
                          join t3 in nPersonnel_Personnel on t2.Personnel_PersonnelKey equals t3.Personnel_PersonnelKey
                          select new
                          {
                              t0.Log_TranferKey,
                              t1.Catalogue_DepartmentKey,
                              t1.Name,
                              t3.ID,
                              RequireUser = t2.Personnel_PersonnelKey,
                              t0.Training_StudentKey,
                              t0.Status
                          }).Distinct();
            return values.Distinct();
        }

        /// <summary>
        /// Lấy tất cả dữ liệu
        /// </summary>
        /// <param name="nLog_Tranfer"></param>
        /// <returns></returns>

        public static IQueryable GetAllData(this Table<Log_Tranfer> nLog_Tranfer)
        {
            var value = from s in nLog_Tranfer
                        orderby
                            s.Log_TranferKey descending
                        select s;
            return value;
        }

        /// <summary>
        /// Lấy dữ liệu user đã yêu cầu
        /// </summary>
        /// <param name="nLog_Tranfer"></param>
        /// <param name="m_strUserKey"></param>
        /// <returns></returns>

        public static IQueryable GetDataWithUserKey(this Table<Log_Tranfer> nLog_Tranfer,
            string m_strUserKey)
        {
            var values = from t in nLog_Tranfer
                         orderby t.Log_TranferKey descending
                         select new
                         {
                             t.Log_TranferKey,
                             t.Training_StudentKey,
                             t.BeforeID,
                             t.AfterID,
                             t.ClassBefore,
                             t.ClassAfter,
                             t.Training_TrainingProgramKeyBefore,
                             t.Training_TrainingProgramKeyAfter,
                             t.Training_TranferTypeKey,
                             t.RequireUser,
                             t.TranferUser,
                             t.TranferTime,
                             t.CensorshipTime,
                             t.Reasons,
                             t.Feedback,
                             t.Permit,
                             t.Refuse,
                             Status = t.Status == false ? "Chờ duyệt" : t.Permit == true ? "Đã duyệt" : "Không duyệt"
                         };
            if ((m_strUserKey != "") || (m_strUserKey != DefaultConfig.EMPTY_GUID))
                values = values.Where(tb => tb.RequireUser.ToString() == m_strUserKey);
            return values;
        }

        /// <summary>
        /// Kiểm tra sinh viên có thể chuyển khóa hoặc ngành hay không
        /// </summary>
        /// <param name="nLog_Tranfer"></param>
        /// <param name="m_strStudentKey"></param>
        /// <returns></returns>

        public static int CheckStudentTranfer(this Table<Log_Tranfer> nLog_Tranfer,
            string m_strStudentKey)
        {
            var values = (from t in nLog_Tranfer
                          orderby t.Log_TranferKey descending
                          where t.Training_StudentKey.ToString() == m_strStudentKey &&
                          t.Status == false
                          select t).Take(1);
            if (values.Count() > 0)
                return int.Parse(values.FirstOrDefault().Training_TranferTypeKey.ToString());
            else
                return 0;
        }

        /// <summary>
        /// Lấy danh sách sinh viên đang chờ duyệt
        /// </summary>
        /// <param name="nLog_Tranfer"></param>
        /// <returns></returns>

        public static IQueryable GetListTranferWait(this Table<Log_Tranfer> nLog_Tranfer)
        {
            var values = from t in nLog_Tranfer
                         where t.Status == false
                         select new
                         {
                             t.Training_StudentKey
                         };
            return values;
        }
    }
}
